// Copyright (c) 2008-2010, Arne Claus
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without modification, are permitted provided that the 
// following conditions are met:
//
// - Redistributions of source code must retain the above copyright notice, this list of conditions and the following 
//   disclaimer.
// - Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following 
//   disclaimer in the documentation and/or other materials provided with the distribution.
// - Neither the name Arne Claus nor the names of any contributors may be used to endorse or promote products
//   derived from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, 
// INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 
// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 
// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 
// WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE 
// USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

#ifndef __INCLUDED_CRL_PIXEL_BUFFER_DESCRIPTOR_H_
#define __INCLUDED_CRL_PIXEL_BUFFER_DESCRIPTOR_H_

#include "CRL_Defines.h"
#include "CBL_Math.h"

CB_SUBNAMESPACE_START(RenderLib)

// -----------------------------------------------------------------------------------
//  PixelFormat Information structs
// -----------------------------------------------------------------------------------

struct SPixelFlag
{
	static const uint32 None       = 0;
    static const uint32 Compressed = 1 << 0;
    static const uint32 Float      = 1 << 1;
    static const uint32 Depth      = 1 << 2;
};

// -----------------------------------------------------------------------------------

struct SPixelFormat
{
	#define ENUM_ITEM(_Name, _Size, _Components, _Flags, _OpenGLFormat, _D3D9Format, _D3D10Format) _Name,
	enum Enum
	{
    	Unused = 0,
		#include "CRL_PixelBufferFormatsEnum.h"
    	NumFormats
	};
	#undef ENUM_ITEM
	
	static const char* s_Names[NumFormats];
    
    uint32 m_Flags;
    uint32 m_Size;
    uint32 m_Components;
};

// -----------------------------------------------------------------------------------
//  CPixelBufferDescriptor
// -----------------------------------------------------------------------------------

class CPixelBufferDescriptor
{
public:
		
	static const SPixelFormat s_PixelBufferFormat[SPixelFormat::NumFormats];
	
public:
	
	enum EDimension
	{
		PixelBuffer1D = 0,
		PixelBuffer2D,
		PixelBuffer3D,
        PixelBufferCube,
	};
	
public:
	
	enum EStorage
	{
		Texture = 0,
    	StaticTexture,
    	DynamicTexture,
    	RenderableTexture,
        NumStorageTypes
	};
	
public:
	
	RENDERLIB_API CPixelBufferDescriptor();
	
	void SetFormat(SPixelFormat::Enum _Format)		{ m_Format = _Format; }
	SPixelFormat::Enum GetFormat() const			{ return m_Format; }
	
	void SetDimension(EDimension _Dimension)		{ m_Dimension = _Dimension; }
	EDimension GetDimension() const					{ return m_Dimension; }
	
	void SetWidth(uint32 _Width)					{ m_Width = _Width; }
	uint32 GetWidth() const							{ return m_Width; }
	
	void SetHeight(uint32 _Height)					{ m_Height = _Height; }
	uint32 GetHeight() const						{ return m_Height; }
	
	void SetDepth(uint32 _Depth)					{ m_Depth = _Depth; }
	uint32 GetDepth() const							{ return m_Depth; }
	
	void SetStorageType(EStorage _StorageType)		{ m_Storage = _StorageType; }
	EStorage GetStorageType() const					{ return m_Storage; }
    
    uint32 GetSizeByte() const						{ return (uint32)(Math::Max(1.0f, m_Width) *
                                                             Math::Max(1.0f, m_Height) * 
                                                             Math::Max(1.0f, m_Depth) * 
                                                             s_PixelBufferFormat[m_Format].m_Size); }
	
    bool IsFloat() 		{ return (s_PixelBufferFormat[m_Format].m_Flags & SPixelFlag::Float)      == SPixelFlag::Float; }
	bool IsCompressed() { return (s_PixelBufferFormat[m_Format].m_Flags & SPixelFlag::Compressed) == SPixelFlag::Compressed; }
	bool IsDepth() 	    { return (s_PixelBufferFormat[m_Format].m_Flags & SPixelFlag::Depth)      == SPixelFlag::Depth; }
	
public:
	
	static const SPixelFormat& GetFormatInfo(SPixelFormat::Enum _Format) { return s_PixelBufferFormat[_Format]; }
	
private:
	
	SPixelFormat::Enum	m_Format;
	EDimension			m_Dimension;
	EStorage			m_Storage;
	
	uint32			m_Width;
	uint32			m_Height;
	uint32			m_Depth;
};

CB_SUBNAMESPACE_END

#endif